/**
 * StringFloatConverter
 *
 * @author Chris Pratt
 *
 * 12/15/2011
 */
package com.anodyzed.onyx.type;

public class StringFloatConverter implements Converter {

  /**
   * Convert Strings (or CharSequence's) to/from Float's
   *
   * @param from Either a CharSequence or a Float 
   * @param to The type to convert to 
   * @return Either a Float or a String
   */
  @Override
  @SuppressWarnings("unchecked")
  public <T> T convert (Object from,Class<T> to) {
    if(from instanceof CharSequence) {
        // Should use to.cast() to avoid @SuppressWarnings, but it doesn't 
        // properly support primitives
      return (T)new Float(from.toString());
    } else if(from instanceof Float) {
      return to.cast(String.valueOf(from));
    }
    throw new ConversionException("{0.class.name} is neither a String (CharSequence) nor a Float",from);
  } //convert

} //*StringFloatConverter
